package com.ywu.file.sys.standard.service;

import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import com.ywu.file.sys.standard.data.YwuFileWhiteListRepository;
import com.ywu.file.sys.standard.dto.YwuFileWhiteListDto;
import com.ywu.file.sys.standard.entity.YwuFileWhiteListEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName YwuFileWhiteListService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/8/17 8:18
 * @Version 1.0
 **/
@Service
public class YwuFileWhiteListService {
    @Autowired
    private YwuFileWhiteListRepository repository;

    /**
    * @Author GroundDemo
    * @Description 分页查询白名单列表
    * @Date 8:33 2024/8/17
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryPageList(YwuFileWhiteListDto dto) {
        dto.setAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        // 查询数量
        int count = repository.queryPageCount(dto);
        // 构建分页参数
        PageService.execPageStartAndEnd(dto.getPageInfo());
        // 查询数据
        List<YwuFileWhiteListEntity> entityList = repository.queryPageList(dto);
        // 构建pageList对象
        PageList<YwuFileWhiteListEntity> resPageList = new PageList<>(dto.getPageInfo().getPageIndex(), dto.getPageInfo().getPageSize(),
                count, entityList);
        return CommonResult.success(resPageList);
    }

    /**
    * @Author GroundDemo
    * @Description 根据appId查询所有的白名单
    * @Date 8:37 2024/8/17
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.util.List<com.ywu.file.sys.standard.entity.YwuFileWhiteListEntity>>
    **/
    public CommonResult<List<YwuFileWhiteListEntity>> queryWhiteListByAppId(YwuFileWhiteListDto dto) {
        YwuFileWhiteListDto search = new YwuFileWhiteListDto();
        search.setAppId(dto.getAppId());
        List<YwuFileWhiteListEntity> entityList = repository.queryAllWhiteList(search);
        if (CollectionUtils.isEmpty(entityList)) {
            return CommonResult.success(new ArrayList<>());
        }
        return CommonResult.success(entityList);
    }

    /**
    * @Author GroundDemo
    * @Description 添加应用文件白名单
    * @Date 8:40 2024/8/17
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> addWhiteListInfo(YwuFileWhiteListDto dto) {
        // 查询数据库中的文件后缀是否已经存在
        YwuFileWhiteListEntity search = new YwuFileWhiteListEntity();
        search.setFileSuffix(dto.getFileSuffix());
        search.setAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        YwuFileWhiteListEntity entity = repository.getEntity(search);
        if (Objects.nonNull(entity)) {
            return CommonResult.error().setErrorCode("file.suffix.has.exist")
                    .setMessage("文件后缀已经存在");
        }
        // 构建插入对象
        YwuFileWhiteListEntity insertEntity = new YwuFileWhiteListEntity();
        BeanUtils.copyProperties(dto, insertEntity);
        insertEntity.setAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        // 生成ID
        insertEntity.setId(TimerIdGenerateUtil.nextId());
        // 生成创建人
        insertEntity.createInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        repository.addSuffixInfo(insertEntity);
        return CommonResult.success(insertEntity.getId());
    }

    /**
    * @Author GroundDemo
    * @Description 修改应用文件白名单
    * @Date 8:44 2024/8/17
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> updateWhiteListInfo(YwuFileWhiteListDto dto) {
        // 查询数据库中的文件后缀是否存在
        YwuFileWhiteListEntity search = new YwuFileWhiteListEntity();
        search.setId(dto.getId());
        search.setAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        YwuFileWhiteListEntity entity = repository.getEntity(search);
        if (Objects.isNull(entity)) {
            return CommonResult.error().setErrorCode("data.not.exist")
                    .setMessage("数据不存在");
        }
        YwuFileWhiteListEntity updateEntity = new YwuFileWhiteListEntity();
        BeanUtils.copyProperties(dto, updateEntity);
        updateEntity.setAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        // 生成更新人和更新时间
        updateEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        repository.updateSuffixInfo(updateEntity);
        return CommonResult.success(updateEntity.getId());
    }

    /**
    * @Author GroundDemo
    * @Description 删除应用文件白名单
    * @Date 8:47 2024/8/17
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteWhiteListInfo(YwuFileWhiteListDto dto) {
        // 查询数据库中的文件后缀是否存在
        YwuFileWhiteListEntity search = new YwuFileWhiteListEntity();
        search.setAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        search.setId(dto.getId());
        YwuFileWhiteListEntity entity = repository.getEntity(search);
        if (Objects.isNull(entity)) {
            return CommonResult.error().setErrorCode("data.not.exist")
                    .setMessage("数据不存在");
        }
        YwuFileWhiteListEntity deleteEntity = new YwuFileWhiteListEntity();
        BeanUtils.copyProperties(dto, deleteEntity);
        repository.deleteSuffixInfoById(deleteEntity);
        return CommonResult.success(null);
    }
}
